package cn.iocoder.yudao.module.member.controller.app.userwithdraw;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
import cn.iocoder.yudao.module.member.controller.admin.userwithdraw.vo.UserWithdrawPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.userwithdraw.vo.UserWithdrawRespVO;
import cn.iocoder.yudao.module.member.controller.admin.userwithdraw.vo.UserWithdrawSaveReqVO;
import cn.iocoder.yudao.module.member.controller.admin.userwithdraw.vo.UserWithdrawUpdateReqVO;
import cn.iocoder.yudao.module.member.controller.app.userwithdraw.vo.AppUserWithdrawSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.userwithdraw.UserWithdrawDO;
import cn.iocoder.yudao.module.member.enums.userwithdraw.WithdrawStatusEnum;
import cn.iocoder.yudao.module.member.service.userwithdraw.UserWithdrawService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP（有关笔试） - 提现")
@RestController
@RequestMapping("/member/user-withdraw")
@Validated
public class AppUserWithdrawController {

    @Resource
    private UserWithdrawService userWithdrawService;

    @PostMapping("/create")
    @Operation(summary = "创建提现")
    public CommonResult<Long> createUserWithdraw(@Valid @RequestBody AppUserWithdrawSaveReqVO createReqVO) {
        createReqVO.setUserId(WebFrameworkUtils.getLoginUserId());
        createReqVO.setStatus(WithdrawStatusEnum.WAITING.getCode());
        return success(userWithdrawService.createUserWithdrawForApp(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新提现")
    @PreAuthorize("@ss.hasPermission('member:user-withdraw:update')")
    public CommonResult<Boolean> updateUserWithdraw(@Valid @RequestBody UserWithdrawUpdateReqVO updateReqVO) {
        userWithdrawService.updateUserWithdraw(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除提现")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:user-withdraw:delete')")
    public CommonResult<Boolean> deleteUserWithdraw(@RequestParam("id") Long id) {
        userWithdrawService.deleteUserWithdraw(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得提现")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:user-withdraw:query')")
    public CommonResult<UserWithdrawRespVO> getUserWithdraw(@RequestParam("id") Long id) {
        UserWithdrawDO userWithdraw = userWithdrawService.getUserWithdraw(id);
        return success(BeanUtils.toBean(userWithdraw, UserWithdrawRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得提现分页")
    public CommonResult<PageResult<UserWithdrawRespVO>> getUserWithdrawPage(@Valid UserWithdrawPageReqVO pageReqVO) {
        pageReqVO.setUserId(WebFrameworkUtils.getLoginUserId());
        PageResult<UserWithdrawRespVO> pageResult = userWithdrawService.getUserWithdrawPage(pageReqVO);
        return success(pageResult);
    }

}